﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ABMath.ModelFramework.Data;
using ABMath.ModelFramework.Models;

namespace ABMath.ModelFramework.Visuals
{
    [Serializable]
    public class ForecastVisual : Visual
    {
        [NonSerialized]
        private TimeSeriesBase<DistributionSummary> distributions;

        [NonSerialized] 
        public MVTimeSeries meansWithQuantiles;

        protected override bool OnInputChanged()
        {
            distributions = TheObject as TimeSeriesBase<DistributionSummary>;

            if (distributions != null)
            {
                meansWithQuantiles = new MVTimeSeries(5);
                meansWithQuantiles.Title = "Forecasts";
                meansWithQuantiles.SubTitle[0] = "Mean";
                meansWithQuantiles.SubTitle[1] = "0.025 Quantile";
                meansWithQuantiles.SubTitle[2] = "0.100 Quantile";
                meansWithQuantiles.SubTitle[3] = "0.900 Quantile";
                meansWithQuantiles.SubTitle[4] = "0.975 Quantile";

                for (int t = 0; t < distributions.Count; ++t)
                {
                    var stuff = new double[5];
                    stuff[0] = distributions[t].Mean;
                    stuff[1] = distributions[t].Mean - 1.96*distributions[t].StdDev;
                    stuff[2] = distributions[t].Mean - 1.28 * distributions[t].StdDev;
                    stuff[3] = distributions[t].Mean + 1.28 * distributions[t].StdDev;
                    stuff[4] = distributions[t].Mean + 1.96 * distributions[t].StdDev;
                    meansWithQuantiles.Add(distributions.TimeStamp(t), stuff, false);
                }

                if (AssociatedControl != null)
                    AssociatedControl.DataHasChanged();
            }

            return (distributions != null);
        }

        public override string GetDescription()
        {
            return "Forecast Results";
        }

        public override string GetShortDescription()
        {
            return "Forecasts";
        }
    }
}
